<?php

namespace app\common\service;

use app\common\model\OperatLog;
use app\http\exception\ApiException;
use randy\tools\units\ExtendQueryHelper;

class OperatLogService extends BaseService
{
    public function getOperatLogList(ExtendQueryHelper $extendQueryHelper, $data)
    {
        $query = OperatLog::build()
                          ->where($extendQueryHelper->where)
                          ->order($extendQueryHelper->sort, $extendQueryHelper->order);
        $list = $extendQueryHelper->is_page ? $query->_paginate() : $query->select();
        return $list;
    }

    public function getOperatLogItem($id)
    {
        return OperatLog::build()
                        ->findOrFail($id);
    }

    public function delOperatLogItem($id)
    {
        $info = OperatLog::find($id);
        return $info->delete();
    }

    public function setOperatLogItem($id, $data)
    {
        $info = OperatLog::find($id);
        $info->save($data);
        return $info;
    }


    public function addOperatLogItem($data)
    {
        $model = new OperatLog();
        $data['operat_log_id'] = snow_id();
        $data['ip'] = get_client_ip();
        $model->save($data);
        return $model;
    }

    public function clear() : bool
    {
        $res = OperatLog::build()
                        ->where('1=1')
                        ->delete();
        return $res;
    }


}
